package com.itheima.health.service.impl;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import com.itheima.health.pojo.Permission;
import java.util.Set;

/**
 * 用户登录服务的实现类
 */
@Slf4j
@DubboService
public class UserServiceImpl implements UserService{
    //注入dao
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public User findByUsername(String username) {

      //使用tk_mybatis完成用户登录
        User user = new User();
        user.setUsername(username);
        User selectOne = userDao.selectOne(user);
        //要查询角色的具体信息
        Set<Role> roles = roleDao.findByUserId(selectOne.getId());
        selectOne.setRoles(roles);
        //查询角色的权限信息
        for (Role role : roles) {
            Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
            role.setPermissions(permissions);
        }
        return selectOne;
    }
}
